import socket
from multiprocessing import Process
from threading import Thread
import hashlib

sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.bind(("127.0.0.1", 5000))
sk.listen(5)


def conn_task(conn):
    while True:
        try:
            get_data = conn.recv(1024)
        except:
            break
        get_data = get_data.decode("utf-8")
        if get_data == "q":
            break
        has = hashlib.sha256()
        has.update(get_data.encode("utf-8"))
        put_data = has.hexdigest()
        get_data += ":"
        conn.send(get_data.encode("utf-8")+put_data.encode("utf-8"))
    print(f"{addr}退出连接！")
    conn.close()


while True:
    conn, addr = sk.accept()
    print(f"{addr}连接成功！")
    t = Thread(target=conn_task, args=(conn,))
    t.start()
